
import javax.sound.sampled.*;
import java.io.IOException;
import java.net.URL;

public class AudioUtil {
    private static final String SHOOT_SOUND = "sounds/explode.wav";
    private static final String EXPLODE_SOUND = "sounds/explosion-under-snow-sfx-230505 (mp3cut.net) (1).wav";
    private static final String BLOOD_SOUND = "sounds/Blood-audio.wav";

    public static void initAudio() {
        // 可在此做音频预加载等初始化操作，当前简单占位
    }

    public static void playShootSound() {
        playSound(SHOOT_SOUND);
    }

    public static void playExplodeSound() {playSound(EXPLODE_SOUND);}

    public static void playBloodSound() {
        playSound(BLOOD_SOUND);
    }

    private static void playSound(String path) {
        try {
            URL url = AudioUtil.class.getClassLoader().getResource(path);
            if (url == null) {
                System.err.println("音效文件不存在: " + path);
                return;
            }
            AudioInputStream ais = AudioSystem.getAudioInputStream(url);
            Clip clip = AudioSystem.getClip();
            clip.open(ais);
            clip.start();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        }
    }
}